﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;

namespace AutoBuyWinform.utils
{
    class HtmlAgilityPackUtil
    {
        /// <summary>
        /// 获取第一个attrName属性中包含content内容的结点
        /// </summary>
        /// <param name="nodes"></param>
        /// <param name="attrName"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static HtmlNode findFirstOneAttrContains(HtmlNodeCollection nodes, string attrName,string content)
        {
            if (nodes != null)
            {
                foreach (HtmlNode node in nodes)
                {
                    if (node.GetAttributeValue(attrName, "").Contains(content))
                    {
                        return node;
                    }
                }
            }

            return null;
                    
        }

        /// <summary>
        /// 获取第一个拥有attrName属性的结点
        /// </summary>
        /// <param name="nodes"></param>
        /// <param name="attrName"></param>
        /// <returns></returns>
        public static HtmlNode findFirstOneHasAttr(HtmlNodeCollection nodes, string attrName)
        {
            if (nodes != null)
            {
                foreach (HtmlNode node in nodes)
                {
                    if (!node.GetAttributeValue(attrName, "").Equals(""))
                    {
                        return node;
                    }
                }
            }

            return null;

        }
    }
}
